package com.yunbiji.demo.controller;

import com.yunbiji.demo.entity.*;

import com.yunbiji.demo.service.*;
import com.yunbiji.demo.util.NoteResult;
import com.yunbiji.demo.util.NoteUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * (CnActivity)表控制层
 *
 * @author makejava
 * @since 2021-06-07 12:53:51
 */
@Controller
@RequestMapping("/activity")
public class CnActivityController {
    /**
     * 服务对象
     */
    @Resource
    private CnActivityService cnActivityService;
    @Resource
    private CnNoteActivityService cnNoteActivityService;
    @Resource
    private CnNotebookService cnNotebookService;
    @Resource
    private CnNoteService cnNoteService;
    @Resource
    private CnUpdownService cnUpdownService;


//    @GetMapping("activity_detail.html")
//    public String goActDetail(){
//        return "activity_detail";
//    }
//    @GetMapping("aa")
//    public String getAct(){
//        return "activity";
//    }
//    @GetMapping("alert_error")
//    public String error(){
//        return "alert/alert_error";
//    }
//    @GetMapping("edit.html")
//    public String toEdit(){
//        return "edit";
//    }
//    @GetMapping("activity.html")
//    public String toActivity(){
//        return "activity";
//    }

    /**
     * 活动详情
     * @return
     */
    @ResponseBody
    @PostMapping("findActivity.do")
    public NoteResult<List<CnActivity>> getActivityList(){
        List<CnActivity>  cnActivities= cnActivityService.queryAll(new CnActivity());

        //构建result
        NoteResult<List<CnActivity>> result = new NoteResult<List<CnActivity>>();
        System.out.println(cnActivities);
        if(cnActivities == null) {
            result.setMsg("未找到数据!");
            result.setStatus(1);
            return result;
        }else {
            result.setStatus(0);
            result.setMsg("加载笔记信息成功");
            result.setData(cnActivities);
            return result;
        }
    }

    /**
     * 笔记
     * @param activityId
     * @param currentPage
     * @return
     */
    @ResponseBody
    @PostMapping("findNoteActivity.do")
    public NoteResult<List<CnNoteActivity>> getNoteActivitys(Integer activityId, Integer currentPage){
        List<CnNoteActivity>  cnActivities= cnNoteActivityService.queryAllByIdLimit(activityId,0,9+currentPage);
        System.out.println("findNoteActivity"+cnActivities);
        //构建result
        NoteResult<List<CnNoteActivity>> result = new NoteResult<List<CnNoteActivity>>();
        if(cnActivities == null) {
            result.setMsg("未找到数据!");
            result.setStatus(1);
            System.out.println("加载笔记信息成功");
            return result;
        }else {
            result.setStatus(0);
            result.setMsg("加载笔记信息成功");
            result.setData(cnActivities);
            System.out.println("加载笔记信息成功");
            return result;
        }
    }

    /**
     * 笔记详情
     * @param noteActivityId
     * @return
     */
    @ResponseBody
    @PostMapping("findNoteActivityDetail.do")
    public NoteResult<CnNoteActivity> getNoteActivityDetail(Integer noteActivityId){

        CnNoteActivity  cnActivities= cnNoteActivityService.queryById(noteActivityId);
//        System.out.println("findNoteActivityDetail"+cnActivities);
        //构建result
        NoteResult<CnNoteActivity> result = new NoteResult<CnNoteActivity>();
        if(cnActivities == null) {
            result.setMsg("未找到数据!");
            result.setStatus(1);
            System.out.println("加载笔记信息成功");
            return result;
        }else {
            result.setStatus(0);
            result.setMsg("加载笔记信息成功");
            result.setData(cnActivities);
//            System.out.println("加载笔记信息成功");
            return result;
        }
    }

    /**
     * 笔记本详情
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("findList.do")
    public NoteResult<List<CnNotebook>> getSelectNoteBook(String userId){
        CnNotebook cnNotebook = new CnNotebook();
        cnNotebook.setCnUserId(userId);
        System.out.println("findList"+cnNotebook);
        List<CnNotebook>  cnActivities= cnNotebookService.queryAll(cnNotebook);
        System.out.println(cnActivities);
        //构建result
        NoteResult<List<CnNotebook>> result = new NoteResult<List<CnNotebook>>();
        if(cnActivities == null) {
            result.setMsg("未找到数据!");
            result.setStatus(1);
            System.out.println("加载笔记信息成功");
            return result;
        }else {
            result.setStatus(0);
            result.setMsg("加载笔记信息成功");
            result.setData(cnActivities);
            System.out.println("加载笔记信息成功");
            return result;
        }
    }

    /**
     * 笔记列表
     * @param noteBookId
     * @return
     */
    @ResponseBody
    @PostMapping("findNote.do")
    public NoteResult<List<CnNote>> getSelectNoteList(String noteBookId){
        CnNote cnNote = new CnNote();
        cnNote.setCnNotebookId(noteBookId);
        System.out.println("findNote"+cnNote);
        List<CnNote>  cnNotes= cnNoteService.queryAll(cnNote);
        System.out.println(cnNotes);
        //构建result
        NoteResult<List<CnNote>> result = new NoteResult<List<CnNote>>();
        if(cnNotes == null) {
            result.setMsg("未找到数据!");
            result.setStatus(1);
            System.out.println("加载笔记信息成功");
            return result;
        }else {
            result.setStatus(0);
            result.setMsg("加载笔记信息成功");
            result.setData(cnNotes);
            System.out.println("加载笔记信息成功");
            return result;
        }
    }

    /**
     * 添加活动笔记
     * @param activityId
     * @param noteId
     * @return
     */
    @ResponseBody
    @PostMapping("addNoteActivity.do")
    public NoteResult<List<CnNoteActivity>> createNoteActivity(Integer activityId,String noteId){


        //构建result
        NoteResult<List<CnNoteActivity>> result = new NoteResult<List<CnNoteActivity>>();

        CnNote cnNote1 = cnNoteService.queryById(noteId);
        System.out.println(noteId);
        System.out.println("addNoteActivity"+cnNote1);
        List<CnNoteActivity> cnNoteActivityList = cnNoteActivityService.queryAll(new CnNoteActivity(null,activityId,noteId,null,null,null,null));
        if(cnNoteActivityList.isEmpty()){
            CnNoteActivity cnNoteActivity = new CnNoteActivity(null,activityId,noteId,0,0,cnNote1.getCnNoteTitle(),cnNote1.getCnNoteBody());
            System.out.println("addNoteActivity"+cnNoteActivity);
            //插入数据
            CnNoteActivity cnNoteActivity1 = cnNoteActivityService.insert(cnNoteActivity);
            System.out.println("addNoteActivity"+cnNoteActivity1);
            //查询活动
            List<CnNoteActivity>  cnActivities= cnNoteActivityService.queryAll(new CnNoteActivity(null,activityId,null,null,null,null,null));
            System.out.println("addNoteActivity"+cnActivities);
            result.setStatus(0);
            result.setMsg("添加成功");
            result.setData(cnActivities);
            System.out.println("添加成功");
            return result;

        }else {
            result.setMsg("添加失败,已经存在此活动的笔记!");
            result.setStatus(1);
            System.out.println("添加失败,已经存在此活动的笔记！");
            return result;
        }




    }

    /**
     * 点赞
     * @param noteActivityId
     * @param noteId
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("upNoteActivity.do")
    public NoteResult<Object> up(Integer noteActivityId,String userId,String noteId){
        System.out.println("noteActivityId="+noteActivityId+noteId+userId);
        //构建result
        NoteResult<Object> result = new NoteResult<Object>();
        //活动笔记
        CnNoteActivity cnNoteActivity = cnNoteActivityService.queryById(noteActivityId);
        cnNoteActivity.setCnNoteActivityUp(cnNoteActivity.getCnNoteActivityUp()+1);

        CnUpdown cnUpdown =new CnUpdown();
        cnUpdown.setCnUserId(userId);
        cnUpdown.setCnNoteId(noteId);

        CnUpdown cnUp = cnUpdownService.queryAll(cnUpdown);

        if (cnUp == null){
            cnUpdown.setCnUp(1);
            cnUpdown.setCnDown(0);
            cnUpdown.setCnCollect(0);
            cnUpdownService.insert(cnUpdown);

            cnNoteActivityService.update(cnNoteActivity);

            result.setStatus(0);
            result.setMsg("点赞成功");
//            result.setData(cnActivities);
//            System.out.println("点赞成功");
//            return result;
        } else if (cnUp.getCnUp()==0 && cnUp.getCnDown()==0){

            cnNoteActivityService.update(cnNoteActivity);

            cnUp.setCnUp(1);
            cnUpdownService.update(cnUp);
            result.setMsg("点赞成功!");
            result.setStatus(0);
//            System.out.println("点赞成功");
//            return result;
        }else if (cnUp.getCnDown()==1){

            result.setMsg("已点踩!不能点赞！");
            result.setStatus(1);
//            System.out.println("已点踩!不能点赞");
//            return result;
        } else{
            cnNoteActivity.setCnNoteActivityUp(cnNoteActivity.getCnNoteActivityUp()-1);
            cnNoteActivityService.update(cnNoteActivity);
            cnUp.setCnUp(0);
            cnUpdownService.update(cnUp);
            result.setMsg("点赞已取消!");
            result.setStatus(1);
//            System.out.println("点赞已取消");
//            return result;
        }
        return result;
    }

    /**
     * 点踩
     * @param noteActivityId
     * @param noteId
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("downNoteActivity.do")
    public NoteResult<Object> down(Integer noteActivityId ,String userId,String noteId){

        //构建result
        NoteResult<Object> result = new NoteResult<Object>();
        //活动笔记
        CnNoteActivity cnNoteActivity = cnNoteActivityService.queryById(noteActivityId);
        cnNoteActivity.setCnNoteActivityDown(cnNoteActivity.getCnNoteActivityDown()+1);

        CnUpdown cnUpdown =new CnUpdown();
        cnUpdown.setCnUserId(userId);
        cnUpdown.setCnNoteId(noteId);
//        System.out.println("noteActivityId="+noteActivityId);
        CnUpdown cnDown = cnUpdownService.queryAll(cnUpdown);
        System.out.println(cnDown);

        if (cnDown == null){
            cnUpdown.setCnDown(1);
            cnUpdown.setCnUp(0);
            cnUpdown.setCnCollect(0);
            cnUpdownService.insert(cnUpdown);

            CnNoteActivity cnNoteActivity1 = cnNoteActivityService.update(cnNoteActivity);

            result.setStatus(0);
            result.setMsg("点踩成功！");
//            result.setData(cnActivities);
//            System.out.println("点踩成功！");
//            return result;
        }else if (cnDown.getCnDown() == 0 && cnDown.getCnUp()==0){
            CnNoteActivity cnNoteActivity1 = cnNoteActivityService.update(cnNoteActivity);
            cnDown.setCnDown(1);
            cnUpdownService.update(cnDown);
            result.setMsg("点踩成功!");
            result.setStatus(0);
//            System.out.println("点踩成功！");
//            return result;
        }else if (cnDown.getCnUp()==1){
            result.setMsg("已点赞!不能点踩！");
            result.setStatus(1);
//            System.out.println("已点赞!不能点踩");
//            return result;
        } else{
            cnNoteActivity.setCnNoteActivityUp(cnNoteActivity.getCnNoteActivityDown()-1);
            cnNoteActivityService.update(cnNoteActivity);
            cnDown.setCnDown(0);
            cnUpdownService.update(cnDown);
            result.setMsg("点踩已取消!");
            result.setStatus(1);
//            System.out.println("点踩已取消!");
//            return result;
        }
        return result;
    }

    @ResponseBody
    @PostMapping("likeActivityNote.do")
    public NoteResult<Object> likeActivityNote(Integer noteActivityId ,String userId,String noteId){
        System.out.println("noteActivityId="+noteActivityId);
        //构建result
        NoteResult<Object> result = new NoteResult<Object>();
        //活动笔记

        CnUpdown cnUpdown =new CnUpdown();
        cnUpdown.setCnUserId(userId);
        cnUpdown.setCnNoteId(noteId);
//        System.out.println("noteActivityId="+noteActivityId);
        CnUpdown cnColl = cnUpdownService.queryAll(cnUpdown);
        System.out.println(cnColl);

        if (cnColl == null){
            cnUpdown.setCnDown(0);
            cnUpdown.setCnUp(0);
            cnUpdown.setCnCollect(1);
            cnUpdownService.insert(cnUpdown);
            result.setStatus(0);
            result.setMsg("收藏成功！");
//            result.setData(cnActivities);
//            System.out.println("点踩成功！");
//            return result;
        }else if (cnColl.getCnCollect() == 0){
            cnColl.setCnCollect(1);
            cnUpdownService.update(cnColl);
            result.setMsg("收藏成功!");
            result.setStatus(0);
//            System.out.println("点踩成功！");
//            return result;
        } else{
            cnColl.setCnCollect(0);
            cnUpdownService.update(cnColl);
            result.setMsg("收藏已取消!");
            result.setStatus(1);
//            System.out.println("点踩已取消!");
//            return result;
        }
        return result;
    }




}